package cn.itcast.fun

/**
  * 偏应用函数使用示例
  * create by zhangtian on 2018/4/2
  */
object ScalaFunDemo01 {

  def main(args: Array[String]): Unit = {
    val result = sum(x => x)(1)(5)
    println(result)
  }

  def sum(f: Int => Int)(a: Int)(b: Int): Int = {
    @annotation.tailrec
    def loop(n: Int, acc: Int) : Int = {
      if (n > b) {
        println(s"n=${n},acc=${acc}")
        acc
      }else {
        println(s"n=${n},acc=${acc}")
        loop(n+1, acc + f(n))
      }
    }

    loop(a, 0)
  }
}
